package com.joinlabs.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.joinlabs.entity.Admin;
import com.joinlabs.entity.Log;
import com.joinlabs.entity.LogDataByLayUITableBean;
import com.joinlabs.entity.PageBean;
import com.joinlabs.entity.User;
import com.joinlabs.entity.UserDataByLayUITableBean;
import com.joinlabs.entity.Weibo;
import com.joinlabs.entity.WeiboDataByLayUITableBean;
import com.joinlabs.entity.WeiboUser;
import com.joinlabs.exception.CustomException;
import com.joinlabs.mapper.AdminMapper;
import com.joinlabs.mapper.LogMapper;
import com.joinlabs.mapper.UserMapper;
import com.joinlabs.mapper.WeiboMapper;
import com.joinlabs.mapper.WeiboUserMapper;
import com.joinlabs.service.IAdminService;

@Service("adminService")
public class AdminServiceImpl implements IAdminService {

	@Resource
	private AdminMapper adminMapper;

	@Resource
	private UserMapper userMapper;
	
	@Resource
	private WeiboUserMapper weiboUserMapper;
	
	@Resource
	private LogMapper logMapper;
	
	@Resource
	private WeiboMapper weiboMapper;

	@Override
	public Admin checkLogin(Admin admin) throws CustomException {
		// TODO Auto-generated method stub

		// 通过名字查询
		Admin successAdmin = adminMapper.findAdminByName(admin.getAdminName());
		System.out.println(successAdmin);

		if (successAdmin == null) {
			throw new CustomException("用户名不存在");
		}

		if (!successAdmin.getAdminPassword().equals(admin.getAdminPassword())) {
			throw new CustomException("密码错误");
		}

		return successAdmin;
	}

	//后台用户
	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		return userMapper.selectAllUsers();
	}

	@Override
	public UserDataByLayUITableBean findAllUserByTableData() {
		// TODO Auto-generated method stub
		UserDataByLayUITableBean bean = new UserDataByLayUITableBean();

		List<User> userlist = findAllUser();
		System.out.println("所有用户：" + userlist);

		bean.setData(userlist);
		bean.setMsg("success");
		bean.setCount(userlist.size());
		bean.setCode(0);

		return bean;
	}

	@Override
	public UserDataByLayUITableBean findUsersByTableDate(PageBean pageBean) {
		// TODO Auto-generated method stub	
		UserDataByLayUITableBean bean = new UserDataByLayUITableBean();

		int totalCount = userMapper.selectUserCount();
		pageBean.calc(totalCount);
		System.out.println(pageBean);

		List<User> userlist = userMapper.selectUsersPage(pageBean);

		bean.setData(userlist);
		bean.setMsg("无数据");
		bean.setCount(totalCount);
		bean.setCode(0);
		return bean;
	}

	//后台微博
	@Override
	public List<WeiboUser> findAllWeibo() {
		// TODO Auto-generated method stub
		return weiboUserMapper.selectAllWeibos();
	}

	@Override
	public WeiboDataByLayUITableBean findAllWeiboByTableData() {
		// TODO Auto-generated method stub
		WeiboDataByLayUITableBean bean = new WeiboDataByLayUITableBean();
		
		List<WeiboUser> weibolist = findAllWeibo();
		
		bean.setData(weibolist);
		bean.setCount(weibolist.size());
		bean.setCode(0);
		bean.setMsg("success");
		return bean;
	}

	@Override
	public WeiboDataByLayUITableBean findWeibosByTableDate(PageBean pageBean) {
		// TODO Auto-generated method stub
		WeiboDataByLayUITableBean bean = new WeiboDataByLayUITableBean();
		
		int totalCount = weiboUserMapper.selectWeiboCount();
		pageBean.calc(totalCount);
		
		List<WeiboUser> weibolist = weiboUserMapper.selectWeibosPage(pageBean);
		
		bean.setData(weibolist);
		bean.setCount(totalCount);
		bean.setCode(0);
		bean.setMsg("无数据");
		return bean;
	}

	//后台文章
	@Override
	public List<Log> findAllLog() {
		// TODO Auto-generated method stub
		return logMapper.selectAllLogs();
	}

	@Override
	public LogDataByLayUITableBean findAllLogByTableData() {
		// TODO Auto-generated method stub
		LogDataByLayUITableBean bean = new LogDataByLayUITableBean();
		List<Log> loglist = findAllLog();
		bean.setData(loglist);
		bean.setCount(loglist.size());
		bean.setCode(0);
		bean.setMsg("success");
		return bean;
	}

	@Override
	public LogDataByLayUITableBean findLogsByTableDate(PageBean pageBean) {
		// TODO Auto-generated method stub
		LogDataByLayUITableBean bean = new LogDataByLayUITableBean();
		
		int totalCount = logMapper.selectLogCount();
		System.out.println("-------------------"+totalCount);
		pageBean.calc(totalCount);
		
		List<Log> loglist = logMapper.selectLogsPage(pageBean);
		
		System.out.println(loglist);
		
		bean.setData(loglist);
		bean.setCount(totalCount);
		bean.setCode(0);
		bean.setMsg("无数据");
		
		System.out.println(bean);
		return bean;
	}

	@Override
	public int delLogByPrimaryKey(long logId) {
		// TODO Auto-generated method stub
		return logMapper.deleteByPrimaryKey(logId);
	}

	@Override
	public Log showLogByPrimaryKey(long logId) {
		// TODO Auto-generated method stub
		Log log = logMapper.selectLogs(logId);
		return log;
	}

	@Override
	public int updateUserByUserId(long userId) {
		// TODO Auto-generated method stub
		return userMapper.updateByUserId(userId);
	}

	@Override
	public UserDataByLayUITableBean findBlackUsersByTableDate(PageBean pageBean) {
		// TODO Auto-generated method stub
		UserDataByLayUITableBean bean = new UserDataByLayUITableBean();

		int totalCount = userMapper.selectBlackUserCount();
		pageBean.calc(totalCount);
		System.out.println(pageBean);

		List<User> userlist = userMapper.selectBlackUsersPage(pageBean);

		bean.setData(userlist);
		bean.setMsg("无数据");
		bean.setCount(totalCount);
		bean.setCode(0);
		return bean;
	}

	@Override
	public User showUserByPrimaryKey(long userId) {
		// TODO Auto-generated method stub
		User user = userMapper.selectUsers(userId);
		return user;
	}

	@Override
	public int delWeiboByPrimaryKey(long weiboId) {
		// TODO Auto-generated method stub
		return weiboUserMapper.deleteByPrimaryKey(weiboId);
	}

	@Override
	public WeiboUser showWeiboByPrimaryKey(long weiboId) {
		// TODO Auto-generated method stub
		WeiboUser weibo = weiboUserMapper.selectWeibos(weiboId);
		return weibo;
	}

	@Override
	public int updataBlackUserByUserId(long userId) {
		// TODO Auto-generated method stub
		return userMapper.updateBlackUserByUserId(userId);
	}

}
